using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MiniAspNetCore
{
    /// <summary>
    /// http监听处理程序
    /// </summary>
    public class HttpListenerFeature : IHttpRequestFeature, IHttpResponseFeature
    {
        /// <summary>
        /// http原始上下文
        /// </summary>
        private readonly HttpListenerContext _context;

        public HttpListenerFeature(HttpListenerContext context) => _context = context;

        /// <summary>
        /// 请求地址
        /// </summary>
        Uri IHttpRequestFeature.Url => _context.Request.Url;

        /// <summary>
        /// 请求header
        /// </summary>
        NameValueCollection IHttpRequestFeature.Header => _context.Request.Headers;

        /// <summary>
        /// 请求body
        /// </summary>
        Stream IHttpRequestFeature.Body => _context.Request.InputStream;

        /// <summary>
        /// 响应body
        /// </summary>
        Stream IHttpResponseFeature.Body => _context.Response.OutputStream;

        /// <summary>
        /// 响应body
        /// </summary>
        NameValueCollection IHttpResponseFeature.Headers => _context.Response.Headers;

        /// <summary>
        /// 响应状体
        /// </summary>
        int IHttpResponseFeature.StatusCode 
        { 
            get => _context.Response.StatusCode;
            set => _context.Response.StatusCode = value; 
        }
    }
}
